﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_AdminHome : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["admin"] == null)
            {
                Response.Redirect("../Default.aspx");
            }
            else
            {
                if (Session["InitType"] != null)
                {
                    switch (Session["InitType"].ToString())
                    {
                        case "重要通知编辑":
                            NoticeManage();
                            break;
                        case "校发文件编辑":
                            DocumentManage();
                            break;
                        default:
                            break;
                    }
                }
            }
        }

    }
    protected void Logoff_Click(object sender, EventArgs e)
    {
        Session.Remove("admin");
        Response.Redirect("../Default.aspx");
    }
    protected void NoticeManagement_Click(object sender, EventArgs e)
    {
        Management.Text = "重要通知列表";
        OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
        NewsListGridView.DataSource = from news in dc.News
                                      join news_to_catalog in dc.News_Categories
                                      on news.News_category equals news_to_catalog.Category_id
                                      where news_to_catalog.Category_id.Equals(1)
                                      select new
                                      {
                                          标识 = news.News_id,
                                          标题 = news.Title
                                      };
        NewsListGridView.DataBind();
        dc.Dispose();

    }
    protected void DocumentManagement_Click(object sender, EventArgs e)
    {
        Management.Text = "校发文件列表";
        OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
        NewsListGridView.DataSource = from news in dc.Documents
                                      join news_to_catalog in dc.Document_Categories
                                      on news.Document_category equals news_to_catalog.Category_id
                                      where news_to_catalog.Category_id.Equals(1)
                                      select new
                                      {
                                          标识 = news.Document_id,
                                          标题 = news.Title
                                      };
        NewsListGridView.DataBind();
        dc.Dispose();

    }
    public void DocumentManage()
    {
        Management.Text = "校发文件列表";
        OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
        NewsListGridView.DataSource = from news in dc.Documents
                                      join news_to_catalog in dc.Document_Categories
                                      on news.Document_category equals news_to_catalog.Category_id
                                      where news_to_catalog.Category_id.Equals(1)
                                      select new
                                      {
                                          标识 = news.Document_id,
                                          标题 = news.Title
                                      };
        NewsListGridView.DataBind();
        dc.Dispose();
    }
    public void NoticeManage()
    {
        Management.Text = "重要通知列表";
        OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
        NewsListGridView.DataSource = from news in dc.News
                                      join news_to_catalog in dc.News_Categories
                                      on news.News_category equals news_to_catalog.Category_id
                                      where news_to_catalog.Category_id.Equals(1)
                                      select new
                                      {
                                          标识 = news.News_id,
                                          标题 = news.Title
                                      };
        NewsListGridView.DataBind();
        dc.Dispose();
    }
    protected void New_Button_Click(object sender, EventArgs e)
    {
        Session.Add("OperationType", "New");
        Session.Add("List", Management.Text);
        Session.Add("NewsType", Management.Text.Replace("列表", "编辑"));
        Response.Redirect("NewsEdit.aspx");
    }
    protected void NewsListGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            SQL.SQLHelper.Delete(Convert.ToInt32(((Label)NewsListGridView.Rows[e.RowIndex].FindControl("News_id")).Text));
        }
        catch (Exception ex)
        {
        }
        switch (Management.Text)
        {
            case "重要通知列表":
                NoticeManage();
                break;
            case "校发文件列表":
                DocumentManage();
                break;
        }
    }
    protected void NewsListGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Session.Add("OperationType", "Edit");
        Session.Add("News_id", ((Label)NewsListGridView.Rows[e.NewEditIndex].FindControl("News_id")).Text);
        Session.Add("List", Management.Text);
        Session.Add("NewsType", Management.Text.Replace("列表", "编辑"));
        Response.Redirect("NewsEdit.aspx");
    }
}
